package com.example.demo5.common.utils;

import cn.hutool.core.io.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MyFileUtil {

    @Value("${file.upload.path}")
    private String path;

    /**
     * 上传文件
     * @param multipartFile
     * @param fileName
     * @return
     * @throws IOException
     */
    public String uploadFile(MultipartFile multipartFile,String fileName) throws IOException {
        String filePath = path + fileName;
        FileUtil.writeBytes(multipartFile.getBytes(),filePath);
        return filePath;
    }


    /**
     * 下载文件
     *
     * @param response
     * @param filePath
     * @return
     * @throws IOException
     */
    public void downloadFile(HttpServletResponse response, String filePath) throws IOException {
        //设置文件路径
//        String filePath = path + fileName;
//        String finenewName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
//        response.setHeader("Content-Disposition", "attachment; filename=" + finenewName);
        ServletOutputStream outputStream = response.getOutputStream();
        FileUtil.writeToStream(filePath,outputStream);

    }



}
